﻿using Currency.Shared.Application.ConfigModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;

namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// 注册中间件
    /// </summary>
    public static class ApplicationBuilderExtension
    {
        /// <summary>
        /// 统一注册Adnc.WebApi通用中间件
        /// </summary>
        /// <param name="app"></param>
        /// <param name="configuration"></param>
        /// <param name="serviceInfo"></param>
        /// <param name="completedExecute"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseAdncMiddlewares(this IApplicationBuilder app, Action<IApplicationBuilder> completedExecute = null)
        {
            var configuration = app.ApplicationServices.GetService<IConfiguration>();
            var environment = app.ApplicationServices.GetService<IHostEnvironment>();
            var serviceInfo = app.ApplicationServices.GetService<IServiceInfo>();

            //if (environment.IsDevelopment()) IdentityModelEventSource.ShowPII = true;

            //DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            //defaultFilesOptions.DefaultFileNames.Clear();
            //defaultFilesOptions.DefaultFileNames.Add("index.html");
            //app.UseDefaultFiles(defaultFilesOptions);

            //app.UseStaticFiles();

            //app.UseCustomExceptionHandler();

            //app.UseHttpsRedirection();

            //app.UseRealIp(x =>
            //{
            //    x.HeaderKeys = new string[] { "X-Forwarded-For", "X-Real-IP" };
            //});

            //app.UseCors(serviceInfo.CorsPolicy);

            app.UseSwagger(c =>
            {
                c.RouteTemplate = $"/swagger/{{documentName}}/swagger.json";
                c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
                {
                    swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"/", Description = serviceInfo.Description } };
                });
            });

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint($"/swagger/{serviceInfo.Version}/swagger.json", $"{serviceInfo.FullName}-{serviceInfo.Version}");
                //c.RoutePrefix = $"{serviceInfo.ShortName}";
            });

            //var healthUrl = configuration.GetConsulSection().Get<ConsulConfig>().HealthCheckUrl;
            //app.UseHealthChecks($"/{healthUrl}", new HealthCheckOptions()
            //{
            //    Predicate = _ => true,
            //    // 该响应输出是一个json，包含所有检查项的详细检查结果
            //    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            //});

            app.UseRouting();//将请求与端点匹配.

            completedExecute?.Invoke(app);

            app.UseAuthentication();//认证

            //var iSSOAuthentication = configuration.IsSSOAuthentication();
            //app.UseSSOAuthentication(iSSOAuthentication);

            app.UseAuthorization();//授权

            //执行匹配的端点.
            app.UseEndpoints(endpoints =>
            {
                //配置全局授权验证
                //endpoints.MapControllers().RequireAuthorization();
                endpoints.MapControllers();
            });

            return app;
        }
    }
}
